﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Eventhere.Work;
using System.IO.IsolatedStorage;

namespace Eventhere.Work
{
    public class Repertory
    {
     
     
     private SortedSet<Contact> contacts;   
     private SortedSet<GroupContact> groupes;

     public SortedSet<Contact> Contacts
     {
         get { return contacts; }
         set { contacts = value; }
     }
     public SortedSet<GroupContact> Groupes
     {
         get { return groupes; }
         set { groupes = value; }
     }

    public Repertory()
    {
		contacts = new SortedSet<Contact>();
		 groupes = new SortedSet<GroupContact>();
    }
    public Repertory(SortedSet<Contact> tree, SortedSet<GroupContact> group)
    {
		contacts = tree;
		 groupes = group;
    }
    
    public void createGroup(String name,SortedSet<Contact> listContacts ){
        GroupContact ungroupe = new GroupContact(name);
        foreach(Contact uncontact in listContacts)
        {
          ungroupe.ContactGroupContact.Add(uncontact);  
        }
        this.Groupes.Add(ungroupe);
    }

    public void createGroup(int Id, String name, SortedSet<Contact> listContacts)
    {
        
        GroupContact ungroupe = new GroupContact(name);
        ungroupe.IdGroup = Id;
        foreach (Contact uncontact in listContacts)
        {
            ungroupe.ContactGroupContact.Add(uncontact);
        }
        this.Groupes.Add(ungroupe);
    }

    public Boolean addContact(Contact unContact)
    {
        this.contacts.Add(unContact);
        return true; // Si le contact a bien été ajouter
    }

    public static Contact GetContactById(int id)
    {
        Contact cont = null;
        if (IsolatedStorageSettings.ApplicationSettings.Contains("Repertoire"))
        {
            foreach (Contact c in ((Repertory)IsolatedStorageSettings.ApplicationSettings["Repertoire"]).Contacts)
            {
                if (c.IdContact == id)
                    cont = c;
            }

        }
        return cont;
        
    }

    public static GroupContact GetGroupById(int id)
    {
        GroupContact group = new GroupContact();
        if (IsolatedStorageSettings.ApplicationSettings.Contains("Repertoire"))
        {
            foreach (GroupContact g in ((Repertory)IsolatedStorageSettings.ApplicationSettings["Repertoire"]).Groupes)
            {
                if (g.IdGroup == id)
                    group = g;
            }

        }
        return group;
    }

    public static void DeleteContactById(int id)
    {
        if (IsolatedStorageSettings.ApplicationSettings.Contains("Repertoire"))
        {
            Contact c = Repertory.GetContactById(id);
            ((Repertory)IsolatedStorageSettings.ApplicationSettings["Repertoire"]).Contacts.Remove(c);
        }
    }

    }
}
